<?php


namespace App\Http\Controllers\Asset;


use App\Contracts\CacheKey;
use App\Models\AssetAffiliation;
use App\Models\AssetBrand;
use App\Models\AssetCategory;
use App\Models\AssetKeeper;
use App\Models\AssetLocation;
use App\Models\AssetStatus;
use App\Models\AssetStockChangeHistoryAction;
use App\Models\AssetVendor;
use Cache;
use Illuminate\Support\Collection;

trait AssetHelper
{


    public function getAllAssetCategories(): Collection
    {
        return Cache::tags([CacheKey::TAG_META])
            ->rememberForever(CacheKey::$KEY_ASSET_CATEGORIES, function () {
                return AssetCategory::all();
            });
    }

    public function getAllAssetStatus(): Collection
    {
        return Cache::tags([CacheKey::TAG_META])->rememberForever(CacheKey::$KEY_ASSET_STATUSES, function () {
            return AssetStatus::all();
        });
    }

    protected function getAssetBrands(): Collection
    {
        return Cache::tags([CacheKey::TAG_META])->rememberForever(CacheKey::$KEY_ASSET_BRANDS, function () {
            return AssetBrand::all();
        });
    }

    protected function getAllAssetVendors(): Collection
    {
        return Cache::tags([CacheKey::TAG_META])->rememberForever(CacheKey::$KEY_ASSET_VENDORS, function () {
            return AssetVendor::all();
        });
    }

    protected function getAllAssetKeepers()
    {
         return Cache::tags([CacheKey::TAG_META])->rememberForever(CacheKey::$KEY_ASSET_KEEPERS, function () {
            return AssetKeeper::all();
        });
    }


    protected function getAllAssetAffiliations(): Collection
    {
        return Cache::tags([CacheKey::TAG_META])->rememberForever(CacheKey::$KEY_ASSET_AFFILIATIONS, function () {
            return AssetAffiliation::all();
        });
    }

    protected function getAllAssetLocations(): Collection
    {
        return Cache::tags([CacheKey::TAG_META])->rememberForever(CacheKey::$KEY_ASSET_LOCATIONS, function () {
            return AssetLocation::all();
        });
    }

    protected function getBrandByName($name)
    {
        return self::getAssetBrands()->first(function ($item) use ($name) {
            return $item->name == $name;
        });
    }

    protected function getAffiliationByName($name)
    {
        return self::getAllAssetAffiliations()->first(function ($item) use ($name) {
            return $item->name == $name;
        });
    }

    protected function getAllPrimaryCategories(): Collection
    {
        $data = $this->getAllAssetCategories()->toArray();
        return collect($data)->filter(function ($item) {
            return empty($item['parent_id']);
        });
    }

    public function getAllAssetStockChangeHistoryActions(): Collection
    {
        return Cache::tags([CacheKey::TAG_META])->rememberForever(CacheKey::$KEY_ASSET_STOCK_CHANGE_HISTORY_ACTIONS, function () {
            return AssetStockChangeHistoryAction::all();
        });
    }


    protected function getAssetStatusNames(): Collection
    {
        return $this->getAllAssetStatus()->pluck('name');
    }

    protected function getPrimaryCategoryByName($name)
    {
        return $this->getAllPrimaryCategories()->first(function ($item) use ($name) {
            return $item['name'] == $name;
        });
    }

    protected function getSubCategoryByPrimaryCategoryIdAndName($primaryCategoryId, $name)
    {
        $primaryCategory = self::getAllPrimaryCategories()->first(function ($item) use ($primaryCategoryId) {
            return $item['id'] == $primaryCategoryId;
        });
        return self::getSubCategoriesByPrimaryCategoryName($primaryCategory['name'])->first(function ($item) use ($name) {
            return $item['name'] == $name;
        });
    }

    protected function getAssetStatusByName($name)
    {
        $data = self::getAllAssetStatus()->toArray();
        return collect($data)->first(function ($item) use ($name) {
            return $item['name'] == $name;
        });
    }

    protected function getKeeperByName($name)
    {
        return $this->getAllAssetKeepers()->first(function ($item) use ($name) {
            return $item->name == $name;
        });
    }

    protected function getVendorByName($name)
    {
        return $this->getAllAssetVendors()->first(function ($item) use ($name) {
            return $item->name == $name;
        });
    }

    protected function getLocationByName($name)
    {
        return $this->getAllAssetLocations()->first(function ($item) use ($name) {
            return $item->name == $name;
        });
    }

    protected function getAllPrimaryCategoryNames(): Collection
    {
        $data = $this->getAllPrimaryCategories();
        $v =   $data->pluck("name");
        return $v;
    }

    protected function getSubCategoriesByPrimaryCategoryName($name): Collection
    {
        $primaryCategory = self::getAllPrimaryCategories()->first(function ($item) use ($name) {
            return $item['name'] == $name;
        });
        if (empty($primaryCategory)) {
            return collect();
        }
        // need to change to array since collection filter will trigger sql execution
        $data =  self::getAllAssetCategories()->toArray();
        $c = collect($data)->filter(function ($item) use ($primaryCategory) {
            return $item['parent_id'] == $primaryCategory['id'];
        });
        return $c;
    }

    protected function getSubCategoriesByName($name): Collection
    {
        return $this->getAllAssetCategories()->filter(function (AssetCategory $category) use ($name){
            return !$category->is_primary && $category->name == $name;
        });
    }

}
